package com.limingru.demoproject.mapper;

import com.limingru.demoproject.domain.User;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserMapper {
    public static Map<String, User> userMap = new HashMap<>();

    static {
        userMap.put("Jack", new User(1, "Jack", "1237890"));
        userMap.put("Tom", new User(2, "Tom", "abc123890yz"));
        userMap.put("Lucy", new User(3, "Lucy", "873904847"));
    }

    public User login(String username, String pwd) {
        User user = userMap.get(username);

        if (user == null) {
            return null;
        }

        if (user.getPwd().equals(pwd)) {
            return user;
        }

        return null;
    }

    public List<User> listUser() {
        List<User> list = new ArrayList<>();
        Collection<User> values = userMap.values();
        list.addAll(values);
        return list;
    }
}
